package com.ygl.log;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.collections.MapUtils;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Date 2022/1/30 16:56
 * @Created yanggld
 * @Description
 */
public class ParamUtil {

    /**
     * 知识库相关请求参数中获取业务的参数信息
     *
     * @param param 原始参数
     * @param key   业务参数key
     * @param <T>
     * @return
     * @throws Exception eg: 新增知识库，获取业务参数list= [{"base_no": "123","base_name": "baseName123"}]
     *                   入参即为
     *                   param：
     *                   {
     *                   "knowledge_base_info": [
     *                   {
     *                   "base_no": "123",
     *                   "base_name": "baseName123"
     *                   }
     *                   ]
     *                   }
     *                   key： knowledge_base_info
     */
    public static <T> List<T> getKnowledgeReqBizParam(String param, String key) {
        Map<String, List<T>> paramMap = str2Map2(param);
        List<T> ts = paramMap.get(key);
        return null;
    }

    public static <K, V> Map<K, V> getMap(String param) throws JsonProcessingException {
        Map<K, V> map = new ObjectMapper().readValue(param, new TypeReference<Map<K, V>>() {
        });
        return map;
    }

    /**
     * string 转 model
     *
     * @param param
     * @param clazz
     * @param <T>
     * @return
     * @throws Exception
     */
    public static <T> T str2T(String param, Class<T> clazz) {
        if (StringUtils.isEmpty(param)) {
            return null;
        }
        try {
            return new ObjectMapper().readValue(param, clazz);
        } catch (IOException e) {
            return null;
        }
    }

    /**
     * map 取 model list
     *
     * @param map
     * @param <T>
     * @return
     * @throws Exception
     */
    public static <T> List<T> getMapTs(Map<String,T> map, String key) {
        if (!MapUtils.isEmpty(map)) {
            Object obj = map.get(key);
            if (obj != null) {
                ObjectMapper mapper = new ObjectMapper();
                try {
                    String objStr = mapper.writeValueAsString(obj);
                    return mapper.readValue(objStr, new TypeReference<List<T>>() {
                    });
                } catch (Exception ex) {
                    ex.printStackTrace();
                    return new ArrayList<T>();
                }
            }
        }
        return new ArrayList<T>();
    }

    /**
     * String 转 map
     *
     * @param param
     * @param <K>
     * @param <V>
     * @return
     * @throws Exception
     */
    public static <K, V> Map<K, V> str2Map(String param) {
        if (StringUtils.isEmpty(param)) {
            return new HashMap<K, V>();
        }
        try {
            return new ObjectMapper().readValue(param, new TypeReference<Map<K, V>>() {
            });
        } catch (IOException e) {
            e.printStackTrace();
            return new HashMap<K, V>();
        }
    }

    public static <K, V> Map<K, List<V>> str2Map2(String param) {
        if (StringUtils.isEmpty(param)) {
            return new HashMap<>();
        }
        try {
            return new ObjectMapper().readValue(param, new TypeReference<Map<K, List<V>>>() {
            });
        } catch (IOException e) {
            e.printStackTrace();
            return new HashMap<>();
        }
    }

}
